import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login.vue";
import Home from "../views/home/Home.vue";
import Welcome from "../views/welcome/welcome.vue";
import Goods from "../views/goods/goods.vue";
import GoodsList from "../views/goods/goodslist.vue";
import Add from "../views/goods/add.vue";
import Users from "../views/users/users.vue";
import Authority from "../views/authority/authority.vue";
import Powers from "../views/authority/powers.vue";
import Order from "../views/order/order.vue";
import Sort from "../views/sort/sort.vue";
import MoneySort from "../views/sort/moneysort.vue";
import AboutMe from "../views/about/aboutme.vue";

Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/login" },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    children: [
      { path: "", redirect: "welcome" },
      { path: "welcome", component: Welcome },
      { path: "goods", component: Goods },
      { path: "goodslist", component: GoodsList },
      { path: "add", component: Add },
      { path: "users", component: Users },
      { path: "authority", component: Authority },
      { path: "powers", component: Powers },
      { path: "order", component: Order },
      { path: "sort", component: Sort },
      { path: "moneysort", component: MoneySort },
      { path: "aboutme", component: AboutMe },
    ],
  },
];

const router = new VueRouter({
  routes,
  // mode:'history'
});

// 挂在路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const tokenStr = sessionStorage.getItem("token");
  if (!tokenStr) return next("/login");
  next();
});

export default router;
